package com.net.oil.push.model;

import com.net.oil.bean.BaseBean;
import com.net.oil.base.BaseModel;
import com.net.oil.http.AppHttpClient;
import com.net.oil.http.custom.BaseObserver;
import com.net.oil.push.bean.NewsDetailBean;
import com.net.oil.push.bean.TechnologySupportDetail;
import com.net.oil.push.contract.PushHandleContract;
import com.net.oil.push.presenter.PushHandlePresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：lch
 * 时间：2018/4/22 0022:下午 16:46
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class PushHandleModel extends BaseModel implements PushHandleContract.Model {

    private PushHandlePresenter presenter;

    public PushHandleModel(PushHandlePresenter presenter) {
        this.presenter = presenter;
        compositeDisposable = new CompositeDisposable();
    }

    @Override
    public void getTechnologySupportDetail(int id) {
        AppHttpClient.ApiService().getTechnologySupportDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<TechnologySupportDetail>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<TechnologySupportDetail> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getTechnologySupportDetailSuccess(tResultBean.getData());
                        }else {
                            presenter.getTechnologySupportDetailFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.getTechnologySupportDetailFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

    @Override
    public void getNewsDetail(int id) {
        AppHttpClient.ApiService().getNewsDetail(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NewsDetailBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseBean<NewsDetailBean> tResultBean) {
                        super.onNext(tResultBean);
                        if (tResultBean != null && tResultBean.getData() != null){
                            presenter.getNewsDetailSuccess(tResultBean.getData());
                        }else {
                            presenter.getNewsDetailFailed();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        presenter.getNewsDetailFailed();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }
}
